﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Browser;
//using System.Windows.Navigation;
using System.Windows.Navigation;
using ZFCNew.Code;
using System.Windows.Threading;

namespace ZFCNew.Controls
{
    public partial class NotificationPopup : UserControl
    {
        private readonly DispatcherTimer _timer;
        private readonly DispatcherTimer _delayTimer;
        private const int PopupOpenTimeInMiliSec = 4500;
        private bool _isOpen;
        private bool _isMouseOnTop;

        private NotificationData _notificationData;
        
        public NotificationPopup()
        {
            // Required to initialize variables
            InitializeComponent();
            _timer = new DispatcherTimer
                         {
                             Interval = TimeSpan.FromMilliseconds(PopupOpenTimeInMiliSec)
                         };
            _timer.Tick += (s, e) => Hide();

            _delayTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(PopupOpenTimeInMiliSec)
            };
            _delayTimer.Tick += (s, e) => Show();

            ExpandBtn.Click += (s, e) => Toggle();
            ContentGrid.MouseEnter += (s, e) => _isMouseOnTop = true;
            ContentGrid.MouseLeave += (s, e) => _isMouseOnTop = false;
        }

        private void Toggle()
        {
            if (_isOpen)
            {
                Hide();
            }
            else
            {
                Show();
            }
        }

        public void Show()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                          {
                                                              VisualStateManager.GoToState(this, "Open", true);
                                                              HtmlHelper.HideLogin();
                                                              _timer.Start();
                                                              if (_delayTimer.IsEnabled)
                                                                  _delayTimer.Stop();
                                                              _isOpen = true;
                                                          });

        }

        public void Hide()
        {
            if (!_isMouseOnTop)
            {
                VisualStateManager.GoToState(this, "Close", true);
                if (!FacebookHelper.IsLoggedIn) HtmlHelper.ShowLogin();
                _timer.Stop();
                _isOpen = false;
            }
        }

        public void SetMessage(NotificationData data)
        {
            this.DataContext = data;
            _notificationData = data;

        }

        public void RemoveMessage(NotificationData data)
        {
            this.DataContext = null;
        }

        private void InternalLinkClick(object sender, RoutedEventArgs e)
        {
            if (_notificationData != null)
            {
                ((MainPage)Application.Current.RootVisual).MainFrame.Navigate(new Uri(_notificationData.InternalLink.Url, UriKind.Relative));
            }
        }


        internal void ShowWithDelay()
        {
            _delayTimer.Start();
        }
    }

    public class NotificationData
    {
        public NotificationType NotificationType { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
        public InternalLink InternalLink { get; set; }
        public InternalLink ExternalLink { get; set; }
    }

    public enum NotificationType
    {
        Error,
        Warning,
        NewFeature,
        FirstTime,
        Success
    }

    public class InternalLink
    {
        public string Name { get; set; }
        public string Url { get; set; }
        public string Target { get { return "_blank"; } }
    }

    public class ExternalLink
    {
        public string Name { get; set; }
        public string Url { get; set; }
        public string Target { get { return null; } }
    }


}